
global name "A0_cash_prepare"
cap log close
log using "${pfile}/${name}.smcl", replace smcl


*Parameteres: YEAR   KINK  SLOPE BEN K-N MI MF   YF DF INF \ YEAR   KINK   SLOPE  BEN   K-N MI MF   YF  DF INF \ YEAR   KINK   SLOPE   BEN   K-N MI MF  YF  DF INF \
local y_2001 "2001, 225.00, 0.8, 180, 1, 4, 3, 2002, 31, 2.021 \ 2001, 297.14, -0.3, 235.31, 2, 4, 3, 2002, 31, 2.021 \ 2001, 495.28, -0.5, 335.88, 3, 4, 3, 2002, 31, 2.021"
local y_2002 "2002, 250.00, 0.8, 200, 1, 4, 3, 2003, 31, 1.877 \ 2002, 330.14, -0.3, 264.11, 2, 4, 3, 2003, 31, 1.877 \ 2002, 550.31, -0.5, 374.19, 3, 4, 3, 2003, 31, 1.877" 
local y_2003 "2003, 300.00, 0.8, 240, 1, 4, 3, 2004, 31, 1.668 \ 2003, 396.18, -0.3, 316.94, 2, 4, 3, 2004, 31, 1.668 \ 2003, 660.37, -0.5, 449.03, 3, 4, 3, 2004, 31, 1.668"
local y_2004 "2004, 325.00, 0.8, 260, 1, 4, 4, 2005, 31, 1.526 \ 2004, 429.20, -0.3, 343.36, 2, 4, 4, 2005, 31, 1.526 \ 2004, 715.40, -0.5, 486.46, 3, 4, 4, 2005, 31, 1.526"
local y_2005 "2005, 375.00, 0.8, 300, 1, 5, 3, 2006, 31, 1.419 \ 2005, 495.23, -0.3, 396.18, 2, 5, 3, 2006, 31, 1.419 \ 2005, 825.46, -0.5, 561.30, 3, 5, 3, 2006, 31, 1.419"
local y_2006 "2006, 437.50, 0.8, 350, 1, 4, 3, 2007, 31, 1.342 \ 2006, 577.77, -0.3, 462.22, 2, 4, 3, 2007, 31, 1.342 \ 2006, 963.04, -0.5, 654.85, 3, 4, 3, 2007, 31, 1.342"
local y_2007 "2007, 475.00, 0.8, 380, 1, 4, 2, 2008, 28, 1.302 \ 2007, 627.29, -0.3, 501.83, 2, 4, 2, 2008, 28, 1.302\ 2007, 1045.48, -0.5, 710.93, 3, 4, 2, 2008, 28, 1.302"
local y_2008 "2008, 518.75, 0.8, 415, 1, 3, 1, 2009, 31, 1.246 \ 2008, 685.06, -0.3, 548.05, 2, 3, 1, 2009, 31, 1.246 \ 2008, 1141.88, -0.5, 776.46, 3, 3, 1, 2009, 31, 1.246"
local y_2009 "2009, 581.25, 0.8, 465, 1, 2, 12, 2009, 31, 1.177 \2009, 767.60, -0.3, 614.08, 2, 2, 12, 2009, 31, 1.177 \ 2009, 1279.46, -0.5, 870.01, 3, 2, 12, 2009, 31, 1.177"
local y_2010 "2010, 637.50, 0.8, 510, 1, 1, 12, 2010, 31, 1.128 \ 2010, 841.88, -0.3, 673.50, 2, 1, 12, 2010, 31, 1.128 \ 2010, 1403.28, -0.5, 954.20, 3, 1, 12, 2010, 31, 1.128"
local y_2011 "2011, 675.00, 0.8, 540, 1, 1, 12, 2011, 31, 1.065 \ 2011, 891.40, -0.3, 713.12, 2, 1, 12, 2011, 31, 1.065 \ 2011, 1485.83, -0.5, 1010.35, 3, 1, 12, 2011, 31, 1.065"
local y_2012 "2012, 777.50, 0.8, 622, 1, 1, 12, 2012, 31, 1    \ 2012, 1026.77, -0.3, 821.41, 2, 1, 12, 2012, 31, 1    \ 2012, 1711.45, -0.5, 1163.75, 3, 1, 12, 2012, 31, 1   "
local y_2013 "2013, 847.50, 0.8, 678, 1, 1, 12, 2013, 31, 0.944  \ 2013, 1090.43, -0.3, 872.34, 2, 1, 12, 2013, 31, 0.944  \ 2013, 1817.56, -0.5, 1235.91, 3, 1, 12, 2013, 31, 0.944"
local y_2014 "2014, 905.00, 0.8, 724, 1, 1, 12, 2014, 31, 0.887  \ 2014, 1151.06, -0.3, 920.84, 2, 1, 12, 2014, 31, 0.887    \ 2014, 1918.62, -0.5, 1304.63, 3, 1, 12, 2014, 31, 0.887"
local y_2015 "2015, 985.00, 0.8, 788, 1, 1, 12, 2015, 31, 0.801  \ 2015, 1222.77, -0.3, 978.26, 2, 1, 12, 2015, 31, 0.801    \ 2015, 2038.15, -0.5, 1385.90, 3, 1, 12, 2015, 31, 0.801"
local y_2016 "2016, 1100, 0.8, 880, 1, 1, 12, 2016, 31, 0.753  \ 2016, 1360.70, -0.3, 1088.56, 2, 1, 12, 2016, 31, 0.753    \ 2016, 2268.05, -0.5, 1542.24, 3, 1, 12, 2016, 31, 0.753"

mat sch_y = ( `y_2001' \ `y_2002' \ `y_2003' \ `y_2004' \ `y_2005' \ `y_2006' \ `y_2007' \ `y_2008' \ `y_2009' \ `y_2010' \ `y_2011' \ `y_2012' \ `y_2013' \ `y_2014' \ `y_2015' \ `y_2016' )
matlist sch_y


// Identify workers found to be at 1st employment spell //

clear
forvalues year = 2002/2016 {
append using "${prais}`year'\2_`year's.dta", keep(tpvincul tipoadm nat cpf dt_hir year)
keep if tipoadm==1
* public employees (30-31-35) are also registered at pasep, which counts for the 5 years criterion to receive the abovo
keep if tpvinculo==10|tpvinculo==20|tpvinculo==30|tpvinculo==31|tpvinculo==35|tpvinculo==50|tpvinculo==60|tpvinculo==70|tpvinculo==90|tpvinculo==95
}

g yf_min = year

gcollapse (min) yf_min , by(cpf) fast
save "A0_cash_entry.dta", replace



// Identifying eligible workers //
clear
forvalues year = 2005/2016 {  // can use up to 2016
append using "${prais}`year'\2_`year's.dta", keep(remmedr remmedia tpvincul nat cpf pis id dt_hir dt_dism year mun tempempr gend causadesli dtbirth tipoadm)
}

* keep only workers with a single contract
sort cpf year
by cpf year: g n_cont = _N
ta n_cont
drop if n_cont > 1

drop if tpvinculo != 10 & tpvinculo != 20 & tpvinculo != 50 & tpvinculo != 60 & tpvinculo != 70 & tpvinculo != 90 & tpvinculo != 95

* keep only workers in the private sector
cap drop if natjuridica <= 2038
cap drop if naturjur <= 2038
cap replace natjuridica = naturjur if natjuridica ==.

* days worked in the year
g wkd = min(mdy(12,31,year),dt_dism) - max(mdy(1,1,year),dt_hir) + 1
label var wkd "days worked elig year"

g x = remmedia - 2
label var x "Earnings around 2 m.w. elig year"

rename id p_id
rename remmedr p_w
rename tpvinculo p_tpvinculo
rename dt_hir p_dt_hir
rename dt_dism p_dt_dism
rename tempempr p_tempempr
*rename ultrem p_ultrem
rename causadesli p_causadesli
rename mun p_mun
rename natjuridica p_natjuridica 
rename tipoadm p_tipoadm

keep x wkd cpf pis p_id year dtbirth p_w p_tpvinculo p_dt_hir p_dt_dism p_tempempr p_causadesli ///
p_mun p_natjuridica p_tpv p_tipoadm  // p_ultrem

*drop if abs(x) > 3
compress
save "A0_cash_elig.dta", replace
*/


*** Building Dataset ****
clear
forvalues year = 2006/2017 {  // can use up to 2016
append using "${prais}`year'\2_`year's.dta", 
keep year id cpf pis nat prev_tempempr tpvinculo causadesli gend school race remmedia remmedr dt_hir dt_dism openjob ocup und dtbirth tempempr hor firm_size1 firm_size2 t_recall recall radiccnpj tipoadm last_lay mun ind sbc uf n_w
keep if causadesli == 11
drop if cpf < 999999 | cpf==.
}

replace year = year - 1
merge m:1 cpf year using A0_cash_elig.dta, keep(3) nogen // information on eligibility
replace year = year + 1
merge m:1 cpf using A0_cash_entry.dta, keepus(yf_min) keep(1 3) nogen // information on labor market entry
replace yf_min = 2000 if yf_min ==.

save A0_cash_base, replace


************ // Defining main sample // *************

use A0_cash_base, clear

keep if year >= 2011 & year <= 2014

keep if abs(x)<=0.8
keep if wkd >= 31  // Abono eligibility (1)
keep if year >= yf_min+5  // Abono eligibility (3)

drop if openjob==1

// setting tenure in months
replace tempempr = tempempr*7/30

// Range - keep all dismissal months for robustness
drop if dt_dism < mdy(6,1,year)
drop if dt_dism > mdy(7,15,year)

keep if tpv == 10
drop if natjuridica <= 2038
keep if hor >= 30
drop if ind == 1

g k = x <=0
g kx = k*x
g x2 = x^2
g kx2 = k*x2


* Generating Bonus Value Variable
g bon_inf = .

forvalues i = 6/15 {
local year = sch_y[1+3*(`i'-1),1]
replace bon_inf = sch_y[1+3*(`i'-1),4] * sch_y[1+3*(`i'-1),10] if year == `year'
}

* compute ui benefit value
g ben = .
g ben_inf = .
foreach i in 11 12 13 14 {

local year = sch_y[1+(`i'-1)*3,1] 			// Year
local mon_i = sch_y[1+(`i'-1)*3,6]			// Initial Month of the UI Schedule
local mw = sch_y[1+(`i'-1)*3,4]			// Yearly Minimum Wage
di "year `year'"
replace ben = sch_y[1+(`i'-1)*3,4]  if remmedr <= sch_y[1+(`i'-1)*3,2] & year == `year' 	// Benefit at the Kink
replace ben = remmedr * 0.8  if remmedr > sch_y[1+(`i'-1)*3,2] & remmedr <= sch_y[2+(`i'-1)*3,2] & year == `year' 	
replace ben = sch_y[2+(`i'-1)*3,4] + (remmedr-sch_y[2+(`i'-1)*3,2]) * 0.5  if remmedr > sch_y[2+(`i'-1)*3,2] & remmedr <= sch_y[3+(`i'-1)*3,2] & year == `year' 
replace ben = sch_y[3+(`i'-1)*3,4] if remmedr > sch_y[3+(`i'-1)*3,2] & year == `year'
replace ben_inf = ben * sch_y[1+3*(`i'-1),10]	if year == `year' 	// Benefit at the Kink
}
drop ben

save A0_cash_main, replace



* Adding data on actual eligibility
use A0_cash_main, clear

rename id id_current
rename p_id id
replace year = year-1

sort year id
merge m:1 year id using "${prais}/2011/1_2011s", keep(1 3) nogen keepus(nome) // recovering workers' names
rename nome nome11
merge m:1 year id using "${prais}/2012/1_2012s", keep(1 3) nogen keepus(nome)
rename nome nome12
merge m:1 year id using "${prais}/2013/1_2013s", keep(1 3) nogen keepus(nome)
rename nome nome13

g nome = ""
replace nome = nome11 if year==2011
replace nome = nome12 if year==2012
replace nome = nome13 if year==2013
drop nome11 nome12 nome12

cap drop cpf32
g double cpf32 = int(cpf/100)
replace cpf32 = cpf32 - int(cpf32/10^6)*10^6
format cpf cpf32 %13.0g

merge m:1 cpf32 nome using "${pabono}/cef2012", gen(m12) keepus(rais) keep(1 3) // merging to information on actual eligibility
merge m:1 cpf32 nome using "${pabono}/cef2013", gen(m13) keepus(rais) keep(1 3)
merge m:1 cpf32 nome using "${pabono}/cef2014", gen(m14) keepus(rais) keep(1 3)

merge m:1 cpf32 nome using "${pabono}/bb2012", gen(m12bb) keepus(rais) keep(1 3)
merge m:1 cpf32 nome using "${pabono}/bb2013", gen(m13bb) keepus(rais) keep(1 3)
merge m:1 cpf32 nome using "${pabono}/bb2014", gen(m14bb) keepus(rais) keep(1 3)

drop rais cpf32 nome

g c12 = m12==3|m12bb==3
g c13 = m13==3|m13bb==3
g c14 = m14==3|m14bb==3

replace year = year+1
rename id p_id
rename id_current id

g cash = (c12&year==2012)|(c13&year==2013)|(c14&year==2014)
replace cash = . if year <=2011

drop m12 m13 m14 m12bb m13bb m14bb

save A0_cash_main, replace

// call script preparing additional variables based on multiple employment spells
do "${dbox}/Brazil_Cash_on_new/Do_restat/A0_cash_prepare_additionalvars.do"

// *** // MAIN ESTIMATION SAMPLE OF CASH GRANT EFFECTS - WORKERS DISMISSED BETWEEN JUNE 1ST AND JULY 15TH // *** //
use "A0_cash_main.dta", clear

keep if abs(x)<=0.4

// Sample selection
drop if dt_dism < mdy(6,1,year)
drop if dt_dism > mdy(7,15,year)

// Preparing vars
replace tempempr = tempempr * 30/7

merge 1:1 year id using A0_additional, keep(1 3) nogen
replace n_und = (mdy(12,31,2017) - dt_dism)/7 if n_und==.
replace und = n_und
drop n_und

replace n_remdezr = . if year(dt_dism + und*7)==year(dt_dism + und*7 + n_jdur*7) & month(dt_dism + und*7 + n_jdur*7)<=11

replace mon12 = 0 if mon12==.
replace mon24 = 0 if mon24==.
replace mon36 = 0 if mon36==.
replace inc12 = 0 if inc12==.
replace inc24 = 0 if inc24==.
replace inc36 = 0 if inc36==.

g emp12 = mon12>0 & mon12!=.
g emp24 = mon24>0 & mon24!=.
g emp36 = mon36>0 & mon36!=.

g totmon = mon12+mon24+mon36
g totinc = inc12+inc24+inc36

g totwage = totinc/totmon
g wage12 = inc12/mon12
g wage24 = inc24/mon24
g wage36 = inc36/mon36

g mig = mun!=n_mun if n_mun!=.
g miguf = int(mun/10^4) != int(n_mun/10^4) if n_mun!=.
g pub = n_natjur <=2038 if n_natjur !=.
g migocup = ocup != n_ocup if n_ocup!=.
g migocup2 = int(ocup/10^3) != int(n_ocup/10^3) if n_ocup!=.

g manager = int(n_ocup/10^5)==1 if n_ocup!=.
g whitec = int(n_ocup/10^5)>=2 & int(n_ocup/10^5)<=4 if n_ocup!=.
g blue = int(n_ocup/10^5)>=5 & int(n_ocup/10^5)<=9 if n_ocup!=.

g migind = ind != n_ind if n_ind!=.
g migind2 = int(sbc/10^4) != int(n_sbc/10^4) if n_sbc!=.

g serv = ind==6
g sales = ind==5
g construc = ind==4
g manu = ind==3

g n_serv = n_ind==6
g n_sales = n_ind==5
g n_construc = n_ind==4
g n_manu = n_ind==3


foreach x in 1 3 6 12 24 36 { // everything monthly
g sur`x' = und > 30/7 * (`x')
g dur`x' = und
replace dur`x' = 30/7 * `x' if und >= 30/7 * `x'
}

g jf20 = und <= 20 // card chetty weber paper - 8-12% lower, 2 monthly earnings, tenure discontinuity
g jf52 = 1 - sur12 // with a 16 percent reduction in re-employment women - 3 monthly earnings , zero effect on men - eitc
// basten 1.2 monthly earnings, -8 p.p. or -14% , age 50 discontinuity

g white = race==2
g age_dism = (dt_dism-dtb)/365

// Labels
label var cash "Eligible for the grant"
label var n_remmedr "Monthly Earnings"
label var n_remdezr "December Earnings"
label var n_jdur "Duration"
label var n_hor "Weekly Hours"
label var pub "Public Sector"
label var manager "Manager"
label var whitec "White Collar"
label var blue "Blue Collar"
label var mig "Different Municipality"
label var miguf "Different State"
label var migocup "Different Occupation"
label var migocup2 "Different Occupation 3 digit"
label var migind "Different Industry"
label var jdur36 "Av. Job Duration"
label var jobs36 "Number of Jobs"
label var totinc "Total Income"
label var inc12 "Total Income - year 1"
label var inc24 "Total Income - year 2"
label var inc36 "Total Income - year 3"
label var totwage "Av. Earnings"
label var wage12 "Av. Earnings - year 1"
label var wage24 "Av. Earnings - year 2"
label var wage36 "Av. Earnings - year 3"
label var totmon "Months Worked"
label var mon12 "Months Worked - year 1"
label var mon24 "Months Worked - year 2" 
label var mon36 "Months Worked - year 3" 
label var dur12 "Unemp. Dur. - year 1" 
label var dur24 "Unemp. Dur. - year 2" 
label var dur36 "Unemp. Dur. - year 3" 
label var sur3  "Survival - 3 months"
label var sur6  "Survival - 6 months"
label var sur12  "Survival - 1 year"
label var sur24  "Survival - 2 years"
label var sur36 "Survival - 3 years"

label var tempempr "Tenure at Lay-off (weeks)"
label var remmedia "Monthly Earnings (min. wages)"
label var p_tempempr "Tenure at Previous Job (weeks)"
label var white "Prob(Worker is white)"
label var age_dism "Age at Dismissal"
label var cash "Eligible"

global var "cash und n_remmedr n_remdezr n_jdur n_hor pub manager whitec blue mig miguf migocup migocup2 migind jdur36 jobs36 totinc inc12 inc24 inc36 totwage wage12 wage24 wage36 totmon mon12 mon24 mon36 emp12 emp24 emp36 dur6 dur12 dur24 dur36 sur3 sur6 sur12 sur24 sur36 tempempr remmedia remmedr p_tempempr p_causadesli school white gend hor age_dism jf20 jf52" //
//n_remmedia 

// Selecting variables
keep year id ${var} serv sales const manu n_serv n_sales n_const n_manu migind2 x cpf school ind dt_dism race gend hor uf dtb sbc lastui // dt_dism tempempr school ind race gend hor year uf remmedr p_causadesli p_tempempr 

// RD variables
cap drop k kx
g k = (x<=0 & x!=.)
g kx = k * x
g x2 = x^2
g kx2 = k*x^2

save A_cash, replace




cap log close